import React, { Component } from 'react';
import { View, ScrollView, } from "react-native";
import { GLOBAL } from '../../../config/global';
import { Block } from './Block';
import navigationSeevice from '../../../components/navigation/navigationSeevice';
import { ToolsTabComponent } from '../app-tools/_component/ToolsTabComponent';
import { SalesReportSection } from '../app-tools/SalesReportSection';
import { CreditReportSection } from '../app-tools/CreditReportSection';
import { TOOLS_INFO } from '../app-tools/_common/service';
import { ToolData, JournalingData, SellData, WarningData } from '../_common/toolData';
import Permission from '../../../utils/permission';
import { API, API_GENERATE } from '../../../config/api';
import HTTP from '../../../utils/http';

const scale = GLOBAL.SCALE;
export class HomeBody extends Component {
	constructor(props) {
		super(props);
		this.state = {
			toolKey: 'TOOLS',
			enableScrollViewScroll: true,
			choice: ''
		}
		this.curIndex = 0;
		this.toolOnoff = false;
		this.JournalingData = [];//授信日报
		this.SellData = [];//销售日报
		this.tabMenuData = {};
		this.warningList = [];//业务预警
	}

	componentWillMount () {
		this._init()
	}

	componentWillReceiveProps (props) {
		if (!props) return;
		//this._init();
	}

	componentDidMount () {
	}

	_init = async () => {
		await this._getTaskType();
		await this._getWarning();
		this.setState({
			ls: true
		})
	}

	_setToolKey (key) {
		this.curIndex = 0;
		this.setState({
			toolKey: key,
		})
	}
	_changeChoice (choice) {
		this.setState({
			choice: choice,
		})
	}
	_setTabIndex (index) {
		this.curIndex = index;
		this.setState({
			ls: true
		})
	}

	//工具类
	_getTaskType = () => {
		let lsTOOLS_INFO = JSON.stringify(TOOLS_INFO);
		lsTOOLS_INFO = JSON.parse(lsTOOLS_INFO);
		//初始状态
		this.YWDJ = null;
		this.KHZJ = null;
		this.PRICE = null;
		this.FKGL = null;
		this.ZYLB = null;
		this.OTHER = null;
		this.JournalingData = [];
		this.SellData = [];
		if (Permission.permissions && Permission.permissions.length) {
			//工具类
			Permission.permissions.map((item) => {
				// console.log(Permission)
				if (ToolData[item]) {
					if (this[ToolData[item].type]) {
						this[ToolData[item].type].push(ToolData[item]);
					} else {
						this[ToolData[item].type] = [];
						this[ToolData[item].type].push(ToolData[item])
					}
					this.toolOnoff = true;
				}
			})
			//报表
			Permission.permissions.forEach((item) => {
				if (JournalingData[item]) {
					this.JournalingData.push(JournalingData[item]);
				}
				if (SellData[item]) {
					this.SellData.push(SellData[item]);
				}
			})
			//tab menu
			if (this.toolOnoff) {
				this.tabMenuData['TOOLS'] = lsTOOLS_INFO['TOOLS'];
				if (!this.JournalingData.length && !this.SellData.length) {
					this.tabMenuData['TOOLS'].excButton = null;
				}
			}
			if (this.JournalingData.length || this.SellData.length) {
				this.tabMenuData['REPORT'] = lsTOOLS_INFO['REPORT'];
				this.tabMenuData['REPORT'].tabs = [];
				if (this.SellData.length) {
					this.tabMenuData['REPORT'].tabs.push({
						value: '销售日报',
						key: 'SALES_REPORT'
					})
					this.curIndex = 1;
				}
				if (this.JournalingData.length) {
					this.tabMenuData['REPORT'].tabs.push({
						value: '授信日报',
						key: 'CREDIT_REPORT'
					})
					this.curIndex = 0;
				}
				if (!this.toolOnoff) {
					this.tabMenuKey = 'REPORT';
					this.tabMenuData['REPORT'].excButton = null;
				}
			}
		}
	}


	_itemGet = (type) => {
		if (this[type]) return;
		this[type] = true;
		HTTP.POST(API_GENERATE(API.INDEX_WARNING_SPLIT), { warnType: type }, { rejectError: false, handerError: true }, 10000).then((data) => {
			delete data.cntName;
			this[type] = false;
			this.warningList && this.warningList.map((item) => {
				if (item.type === type) {
					return Object.assign(item, data);
				}
			})
			this.setState({ ls: true })
		}, (err) => {
			console.log('失败-bbbbb', err)
		})
	}

	_getWarning = () => {
		this.warningList = [];
		if (Permission.permissions && Permission.permissions.length) {
			Permission.permissions.map((item) => {
				if (item == 'YWYJ001') { // 业务预警 - 资质到期
					this.warningList.push(WarningData['qualification']);
					this._itemGet('qualification');
				} else if (item == 'YWYJ002') { // 业务预警 - 授信到期
					this.warningList.push(WarningData['creditInfo']);
					this._itemGet('creditInfo');
				} else if (item == 'YWYJ003') { //// 业务预警 - 订单逾期
					this.warningList.push(WarningData['orderInfo']);
					this._itemGet('orderInfo');
				} else if (item == 'CLM001') {  //意向认领
					this.warningList.push(WarningData['claim']);
					this._itemGet('claim');
				} else if (item == 'YWYJ004') { //业务预警 - 审批中业务
					this.warningList.push(WarningData['auditing']);
					this._itemGet('auditing');
				} else if (item == 'YWYJ005') { // 业务预警-保证金超期
					this.warningList.push(WarningData['salePrepay']);
					this._itemGet('salePrepay');
				} else if (item == 'YWYJ006') { //业务预警-销售未收款
					this.warningList.push(WarningData['saleUncollected']);// 销售未收款
					this._itemGet('saleUncollected');
				}
			})
		}
		// 特殊不用权限
		this.warningList.push(WarningData['customer']);// 商机客户
		this._itemGet('customer');
	}


	//应用工具/数据日报
	_renderToolsView () {
		let { toolKey, enableScrollViewScroll, choice } = this.state;
		if (toolKey === 'REPORT') {
			//日报
			if ((this.tabMenuData[toolKey].tabs[this.curIndex] && this.tabMenuData[toolKey].tabs[this.curIndex].key) === 'CREDIT_REPORT') {
				return (
					<View>
						{/* 授信日报 */}
						<CreditReportSection
							reponsederCapture={() => {
								this.setState({ enableScrollViewScroll: false });
								if (this._myScroll && this._myScroll.contentOffset === 0
									&& this.state.enableScrollViewScroll === false) {
									this.setState({ enableScrollViewScroll: true });
								}
							}}
							data={{ menuData: this.JournalingData, choice: choice }}
							goRankingRpt={() => navigationSeevice.navigate('ReportIndexPage', { rptId: 'RPT_MOBILE_1005', choice })} />
					</View>
				)
			} else if ((this.tabMenuData[toolKey].tabs[this.curIndex] && this.tabMenuData[toolKey].tabs[this.curIndex].key) === 'SALES_REPORT') {
				return (
					<View>
						{/* 销售日报 */}
						<SalesReportSection data={{ menuData: this.SellData, choice: choice }} />
					</View>
				)
			}
		} else if (toolKey === 'TOOLS') {
			//应用工具
			return this.toolOnoff ? (<View style={{ marginTop: 20 * scale, paddingBottom: 20 * scale }}>
				{this.YWDJ && this.YWDJ.length ? <Block sourceData={{ data: this.YWDJ, title: '业务管理' }} /> : null}
				{this.KHZJ && this.KHZJ.length ? <Block sourceData={{ data: this.KHZJ, title: '客户资金管理' }} /> : null}
				{this.PRICE && this.PRICE.length ? <Block sourceData={{ data: this.PRICE, title: '价格管理' }} /> : null}
				{this.FKGL && this.FKGL.length ? <Block sourceData={{ data: this.FKGL, title: '风控管理' }} /> : null}
				{this.ZYLB && this.ZYLB.length ? <Block sourceData={{ data: this.FKGL, title: '资源管理' }} /> : null}
				{this.OTHER && this.OTHER.length ? <Block sourceData={{ data: this.OTHER, title: '其它' }} /> : null}
			</View>) : null
		}
	}

	render () {
		let { choice } = this.state;
		return (
			<View style={{ flex: 1, marginTop: 20 * scale }} onStartShouldSetResponderCapture={() => {
				this.setState({ enableScrollViewScroll: true });
			}} >
				{/* <ScrollView scrollEnabled={enableScrollViewScroll}
					ref={myScroll => (this._myScroll = myScroll)}> */}
				{this.warningList.length ? <Block sourceData={{ data: this.warningList, title: '业务看板' }} /> : null}
				{/**应用报表模块 头部切换*/}
				<View>
					<ToolsTabComponent menuData={this.tabMenuData} toolKey={this.state.toolKey} curIndex={this.curIndex}
						setTabIndex={(index) => this._setTabIndex(index)}
						setToolKey={(toolKey) => this._setToolKey(toolKey)}
						changeChoice={(choice) => this._changeChoice(choice)}
					/>
					{this._renderToolsView()}
				</View>

				{/* </ScrollView> */}
			</View>
		)
	}
}